<script type="text/javascript">
var timeOutObj = null;
var address_type = 0;
function getArea(level,sqlTable,type)
{
	$('#myModal').modal('show');
	if(type != undefined){
		address_type = type;
		if(address_type == 'add'){
			$('.address_info_city').children('span').html('未选择').removeAttr('area_id');
			$('#my_address_name').val('');
		}else{
			var province = $('#province_'+address_type).val();
			var city = $('#city_'+address_type).val();
			var area = $('#area_'+address_type).val();
			var my_address_name = $('#my_address_name_'+address_type).val();
			$.post('<?php echo site_url('common/getAreaForId'); ?>',{'province':province,'city':city,'area':area},function(data){
				$('.address_info_city').children('span').eq(0).html(data['province']['name']).attr('area_id',data['province']['id']);
				$('.address_info_city').children('span').eq(1).html(data['city']['name']).attr('area_id',data['city']['id']);
				$('.address_info_city').children('span').eq(2).html(data['area']['name']).attr('area_id',data['area']['id']);
				$('#my_address_name').val(my_address_name);
			},'json');
		}
	}
	$('.select_box_select').hide();
	$('.select_box_loding').fadeIn('fast',function(){
		var bgDom = '';
		if(sqlTable != 'province'){
			if($('.select_box_select ul').attr('level_type') == 'area'){
				bgDom = '<li onclick="setArea(this,\'back\')" area_type="province" area_id="0">返回上一级</li>';
				}else{
					bgDom = '<li onclick="setArea(this,\'back\')" area_type="'+$('.select_box_select ul').attr('level_type')+'" area_id="'+$('.select_box_select ul').attr('level_parent')+'">返回上一级</li>';
					}
			};
		$('.select_box_select ul').html(bgDom).attr({'level_parent':level,'level_type':sqlTable});
		$.post('<?php echo site_url('common/getArea'); ?>',{'fatherId':level,'sqlTable':sqlTable},function(data)
			{
			$.each(data,function(i,j){
				$('.select_box_select ul').append('<li onclick="setArea(this,\'\')" area_type="'+sqlTable+'" area_id="'+j[sqlTable+'ID']+'">'+j[sqlTable]+'</li>');
				});
			$('.select_box_loding').hide();
			$('.select_box_select').fadeIn();
			},'json');
	});
}
function setArea(target,type)
{
	var next_type = '';
	var area_type = $(target).attr('area_type');
	var area_id = $(target).attr('area_id');
	var area_name = $(target).html();
	if(type == 'back'){
		getArea(area_id,area_type);
		return;
	}
	if(area_type == 'province'){
		$('.address_info_city').children('span').html('未选择');
		var n = 0;
		next_type = 'city';
	}else if(area_type == 'city'){
		var n = 1;
		next_type = 'area';
	}else if(area_type == 'area'){
		var n = 2;
	}
	$('.address_info_city').children('span').eq(n).html(area_name);
	$('.address_info_city').children('span').eq(n).attr('area_id',area_id);
	if(area_type == 'area')
	{
		$(target).siblings().removeAttr('style');
		$(target).css({'background-color':'#666','color':'#fff'})
	}
	else
	{
		getArea(area_id,next_type);
	}
}
function edit_address()
{
	var isEmpty = 0;
	var address_str = '';
	$.each($('.address_info_city span'),function(i,j){
		if($(j).html() != '未选择'){
			address_str+=$(j).html()+' ';
			}else{
				isEmpty = 1;
				}
		});
	if(isEmpty == 1){
		$('.modal-body').prepend('<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">×</button><strong>提示!</strong>  请选择正确的省、市、区.</div>');
	}
	if($('#my_address_name').val() == ''){
		isEmpty = 1;
		$('.modal-body').prepend('<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">×</button><strong>提示!</strong>  发货人姓名不能为空.</div>');
	}
	timeOutObj = setTimeout(removeAlert,2000);
	if(isEmpty == 0){
		if(address_type == 'add'){
			var myAddress_id = 0;
			var name = $('#my_address_name').val();
			var province = $('.address_info_city span').eq(0).attr('area_id');
			var city = $('.address_info_city span').eq(1).attr('area_id');
			var area = $('.address_info_city span').eq(2).attr('area_id');
			var address_info = $('.address_info_city span').eq(0).html()+'  '+$('.address_info_city span').eq(1).html()+'  '+$('.address_info_city span').eq(2).html();
			var is_auto = 0;
			$.post('<?php echo site_url("member/member_center/addMyAddress");?>',{'name':name,'province':province,'city':city,'area':area,'address_info':address_info,'is_auto':is_auto},function(data){
				myAddress_id = data*1;
				if(myAddress_id == 0){
					alert('操作失败，请重试...');
					return false;
				}
				var dom = '<div class="well"><blockquote><div class="btn-group" id="btn-group_'+myAddress_id+'">'
				    +'<button class="btn" onclick="setAotuAddress('+myAddress_id+');">当前默认发货地址</button>'
	                +'<button class="btn" onclick="getArea(0,\'province\','+myAddress_id+');">编辑</button>'
	                +'<button class="btn" onclick="deleteMyAddress('+myAddress_id+')">删除</button>'
	                +'<button class="btn" onclick="getArea(0,\'province\',\'add\');">增加发货地址</button>'
	            	+'</div><div style="height: 1px;margin: 10px 0px;overflow: hidden;background-color: #e5e5e5;border-bottom: 1px solid #ffffff;"></div><div class="my_address_body" id="address_body_'+myAddress_id+'">'
	            	+'<input type="hidden" id="province_'+myAddress_id+'" value="'+province+'"><input type="hidden" id="city_'+myAddress_id+'" value="'+city+'"><input type="hidden" id="area_'+myAddress_id+'" value="'+area+'"><input type="hidden" id="my_address_name_'+myAddress_id+'" value="'+name+'">'
	            	+'<p><span>　发货人：</span>'+name+'</p><p><span>发货地址：</span>'+address_info+'</p></div></blockquote></div>';
	            if($('#my_address_box .well').length > 0){
	            	$('#my_address_box').append(dom);
	            }else{
	            	$('#my_address_box').html(dom);
	            }
				$('#myModal').modal('hide');
			});
		}else{
			var name = $('#my_address_name').val();
			var province = $('.address_info_city span').eq(0).attr('area_id');
			var city = $('.address_info_city span').eq(1).attr('area_id');
			var area = $('.address_info_city span').eq(2).attr('area_id');
			var address_info = $('.address_info_city span').eq(0).html()+'  '+$('.address_info_city span').eq(1).html()+'  '+$('.address_info_city span').eq(2).html();
			$.post('<?php echo site_url("member/member_center/editMyAddress");?>',{'id':address_type,'name':name,'province':province,'city':city,'area':area,'address_info':address_info});
			$('#province_'+address_type).val(province);
			$('#city_'+address_type).val(city);
			$('#area_'+address_type).val(area);
			$('#my_address_name_'+address_type).val(name);
			$('#address_body_'+address_type).children('p').eq(0).html('<span>　发货人：</span>'+name);
			$('#address_body_'+address_type).children('p').eq(1).html('<span>发货地址：</span>'+address_info);
			$('#myModal').modal('hide');
		}
	}
}
function removeAlert(){
	clearTimeout(timeOutObj);
	$('.alert').last().fadeOut('slow',function(){
		if($('.alert').length > 0){
			timeOutObj = setTimeout(removeAlert,2000);
		}
		$(this).remove();
	});
}
function deleteMyAddress(id){
	if(confirm('改操作不可逆！！！确定执行？')){
		$.post('<?php echo site_url("member/member_center/deleteMyAddress"); ?>',{'id':id},function(data){
			if(data == 1){
				$('#address_body_'+id).parents('.well').remove();
				if($('#my_address_box .well').length == 0){
					$('#my_address_box').html('<span style="display: block;font-size: 30px;line-height: 50px;text-align: center;padding-top: 25px;">当前发货地址栏还是空的哦...^^!</br>添加一个吧<button class="btn"  onclick="getArea(0,\'province\',\'add\');">增加发货地址</button></span>')
				}
			}else{
				alert('服务器通讯故障，请重试...');
			}
		});
	}
}
function setAotuAddress(id)
{
	$.post('<?php echo site_url("member/member_center/editMyAddress");?>',{'id':id,'is_auto':1},function(){
		$('button[disabled]').removeAttr('disabled');
		$('#btn-group_'+id).children('button').eq(0).attr('disabled','');
	});
}
</script>
<div class="modal hide fade in" id="myModal" style="top: 50%;">
<style>
.select_box{width: 200px;height: 100px;float: left;border: 1px #ccc solid;}
.select_box_select{height: 100px;overflow-y: scroll;overflow-x: hidden;display: none;}
.select_box_select ul{padding: 0px;margin: 0px;list-style: none;}
.select_box_select ul li{line-height: 25px;padding: 0px 20px;cursor: pointer;margin: 0px 5px;}
.select_box_select ul li:hover{background-color: rgb(102, 102, 102);color: rgb(255, 255, 255);}
.select_box_loding{width: 200px;height: 100px;background: url(img/loading_s.gif) no-repeat 85px 35px;display: none;}
.address_info{width: 270px;height: 90px;float: left;border: 1px #ccc solid;margin-left: 15px;padding: 5px 20px;}
.address_info_city{padding: 5px 10px;}
.address_info_city span{line-height: 25px;padding-right: 10px;}
.address_info textarea{width: 260px;height: 45px;margin-bottom: 0;}
.my_address_name{float: left;padding-top: 15px;}
.my_address_name label{float: left;line-height: 25px;}
</style>
   <div class="modal-header">
       <button type="button" class="close" data-dismiss="modal">×</button>
       <h3>发货地址设置</h3>
   </div>
   <div class="modal-body">
      <div class="select_box">
      	<div class="select_box_select">
      		<ul level_type="province" level_parent="0">
      		</ul>
      	</div>
      	<div class="select_box_loding"></div>
      </div>
      <div class="address_info">
      	<div class="address_info_city">
      		<h5 style="float: left;line-height: 24px;">地址：</h5><span area_type="province">未选择</span><span area_type="city">未选择</span><span area_type="area">未选择</span>
      	</div>
      	<div class="my_address_name">
          <label for="my_address_name">发货人姓名：</label>
          <input type="text" class="input-xlarge" id="my_address_name" style="width: 150px;">
      	</div>
      </div>
   </div>
   <div class="modal-footer">
      <a href="#" class="btn" data-dismiss="modal">关闭</a>
      <a href="javascript:void(0);" onclick="edit_address();" class="btn btn-primary">确认发货地址</a>
   </div>
</div>
<div class="span10">
          <div class="row-fluid">
            <div class="span12 box">
            <?php echo (isset($tips))?$tips:'';?>
                <div class="box-head">
                    <h3><i class="icon-white icon-plane" style="margin-top: 7px;"></i> 发货地址</h3>
                </div>
                <div class="box-content">
                    <div class="content-inner">
                    	<style>blockquote{margin: 0;}blockquote p{font-size: 12px;}blockquote p span{}</style>
                        <div class="content-settings" id="my_address_box">
                        <?php if(count($myAddressData) < 1)
                        {?>
                        	<span style="display: block;font-size: 30px;line-height: 50px;text-align: center;padding-top: 25px;">当前发货地址栏还是空的哦...^^!</br>添加一个吧<button class="btn"  onclick="getArea(0,'province','add');">增加发货地址</button></span>
                        <?php }else{
                        	foreach ($myAddressData as $k=>$v):;?>
                        	<div class="well">
                        		<blockquote>
                        			<div class="btn-group" id="btn-group_<?php echo $v['id'];?>">
                        			  <button class="btn" onclick="setAotuAddress(<?php echo $v['id'];?>);" <?php echo ($v['is_auto'] == 0)?'':'disabled';?>>当前默认发货地址</button>
	                                  <button class="btn" onclick="getArea(0,'province',<?php echo $v['id'];?>);">编辑</button>
	                                  <button class="btn" onclick="deleteMyAddress(<?php echo $v['id'];?>);">删除</button>
	                                  <button class="btn"  onclick="getArea(0,'province','add');">增加发货地址</button>
	                                </div>
                                    <div style="height: 1px;margin: 10px 0px;overflow: hidden;background-color: #e5e5e5;border-bottom: 1px solid #ffffff;"></div>
                                    <div class="my_address_body" id="address_body_<?php echo $v['id'];?>">
                                    	<input type="hidden" id="province_<?php echo $v['id'];?>" value="<?php echo $v['province'];?>" />
                                    	<input type="hidden" id="city_<?php echo $v['id'];?>" value="<?php echo $v['city'];?>" />
                                    	<input type="hidden" id="area_<?php echo $v['id'];?>" value="<?php echo $v['area'];?>" />
                                    	<input type="hidden" id="my_address_name_<?php echo $v['id'];?>" value="<?php echo $v['name'];?>" />
                                    	<p><span>　发货人：</span><?php echo $v['name'];?></p>
                                    	<p><span>发货地址：</span><?php echo $v['address_info'];?></p>
                                    </div>
                        		</blockquote>
                        	</div>
                        	<?php endforeach;
                        }?>
                        </div>
                    </div><!-- content-inner -->
                </div>
            </div>
          </div> <!-- end row -->
        </div><!--/span-->
      </div><!--/row-->